#include <stdio.h>
#include <unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>


int main(){
  pid_t id=fork();
  if(id==0){
    //child
    int cnt =5;
    while(cnt){
      printf("I am child process,pid:%d,ppid:%d,cnt:%d\n",getpid(),getppid(),cnt);
      cnt--;
    }
    sleep(1);
    exit(1);
  }
  int status=0;
  pid_t rid=waitpid(id,&status,0);
  if(rid>0){
    //status & 0x7f:0000 0000 0000 0000 0000 0000 0111 1111
   //printf("wait success,rid:%d,status:%d,exit signo:%d, exit code:%d\n",rid,status,status&0x7F,(status>>8)&0xFF);  
  if(WIFEXITED(status)){ //当信号为0，正常终止子进程，为真
      printf("wait success,rid:%d,status:%d,exit code:%d\n",rid,status,WEXITSTATUS(status));
    }
  else{
      printf("child process error!\n");
    }
  }
  return 0;
}
